var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

// 导入swagger配置
const swaggerInstall = require('./utils/swagger');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
// 引入login路由
let loginRouter = require('./routes/login.js')
// 引入product路由
let productRouter = require('./routes/product.js')
//app.js引入menus路由
let menusRouter = require('./routes/menus.js')
//导入cors中间件
const cors = require('cors');

const categoryRouter = require('./routes/category');

const customeRouter = require('./routes/custome');

const ordersRouter = require('./routes/orders');

const categoryinfoRouter = require('./routes/categoryinfo');

const log_qxRouter = require('./routes/log_qx');

var app = express();

// 处理跨域
app.use(cors({
  origin: 'http://localhost:8080',
  methods: ['GET', 'POST'],
  allowedHeaders: ['Content-Type', 'Authorization']
}));

//调用swaggerInstall()
swaggerInstall(app)

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/users', usersRouter);
// 挂载login路由
app.use('/api', loginRouter)
app.use('/api', productRouter)
app.use('/api', menusRouter)
app.use('/api', categoryRouter);
app.use('/api', customeRouter);
app.use('/api', ordersRouter);
app.use('/api', categoryinfoRouter);
app.use('/api', log_qxRouter)
// catch 404 and forward to error handler
app.use(function (req, res, next) {
  next(createError(404));
});

// error handler
app.use(function (err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
